package com.guigu.community.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.community.mapper.SuppliersMapper;
import com.guigu.community.mapper.UsersMapper;
import com.guigu.community.pojo.Products;
import com.guigu.community.pojo.Suppliers;
import com.guigu.community.pojo.Users;
import com.guigu.community.service.SuppliersService;
import com.guigu.community.vo.QuoteVo;
import com.guigu.community.vo.SuppliersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SuppliersServiceImpl extends ServiceImpl<SuppliersMapper, Suppliers> implements SuppliersService {
    @Autowired
    private SuppliersMapper suppliersMapper;
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public PageInfo<Suppliers> queryVo(Integer pageNo, Integer pageSize, SuppliersVo vo) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Suppliers> pageInfo = new PageInfo<>(suppliersMapper.selectVo(vo));
        return pageInfo;
    }

    @Override
    public PageInfo<Suppliers> queryByVo(Integer pageNo, Integer pageSize, SuppliersVo vo) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Suppliers> pageInfo = new PageInfo<>(suppliersMapper.selectByVo(vo));
        return pageInfo;
    }

    @Override
    public Byte getSupplierStatusById(Integer supplierId) {
        Suppliers supplier = suppliersMapper.selectById(supplierId);
        if (supplier != null) {
            return supplier.getStatus();
        }
        return null;
    }

    @Override
    public boolean approveSupplier(Integer supplierId) {
        Suppliers supplier = suppliersMapper.selectById(supplierId);
        if (supplier != null) {
            // 假设状态 1 表示通过审核
            supplier.setStatus((byte) 1);
            int result = suppliersMapper.updateById(supplier);
            return result > 0;
        }
        return false;
    }

    @Override
    @Transactional // 开启事务，确保两个更新操作要么都成功，要么都失败
    public boolean approveSupplierAndUpdateUser(Integer supplierId) {
        Suppliers supplier = suppliersMapper.selectById(supplierId);
        if (supplier != null) {
            // 更新 suppliers 表的 status 为 1
            supplier.setStatus((byte) 1);
            int supplierResult = suppliersMapper.updateById(supplier);

            // 根据 supplier 的 userId 更新 users 表的 Is_vendor 为 1
            Users user = usersMapper.selectById(supplier.getUserId());
            if (user != null) {
                user.setIsVendor(1);
                int userResult = usersMapper.updateById(user);
                return supplierResult > 0 && userResult > 0;
            }
        }
        return false;
    }

    @Override
    public boolean provideQuote(QuoteVo quoteVo) {
        return false;
    }


    @Override
    public boolean rejectSupplier(Integer supplierId) {
        Suppliers supplier = suppliersMapper.selectById(supplierId);
        if (supplier != null) {
            // 假设状态 2 表示拒绝审核
            supplier.setStatus((byte) 2);
            int result = suppliersMapper.updateById(supplier);
            return result > 0;
        }
        return false;
    }

}